import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Lei
 * @create 2022-05-22 18:12
 */
public class ViewCreate extends View<ControllerCreate> {
    private JTextField t1;
    private JTextField t2;
    private JComboBox<String> cb;

    public ViewCreate(Library m, ControllerCreate c) {
        super(m, c);
        t1 = new JTextField();
        add(t1);
        t2 = new JTextField();
        add(t2);
        cb = new JComboBox<>(new String[]{"Lender", "Borrower"});
        add(cb);

        setLayout(new GridLayout(4, 1));
        Button tell_me_the_book_number = new Button("Create");
        tell_me_the_book_number.addActionListener(new MyActionListener(this));
        add(tell_me_the_book_number);

        //Display the window.
        pack();
        setVisible(true);
        cb.setVisible(true);
    }


    @Override
    public void update() {

    }

    class MyActionListener implements ActionListener {


        private JFrame parent;

        public MyActionListener(JFrame parent) {
            this.parent = parent;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = t1.getText();
            String number = t2.getText();
            int selectedIndex = cb.getSelectedIndex();
            String res = c.create(name, number, selectedIndex);
            JOptionPane.showMessageDialog(parent, res);
        }
    }
}
